//
//  SafeCheckTaskVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/12/23.
//

import Foundation
import UIKit

class SafeCheckTaskVC: SubLevelViewController {
    let listV = SecurityTaskDetailView()
    
    var modal: LzglbaListModel!

    var baseInfos: [BaseInfoModel] = []

    init(withModal _modal: LzglbaListModel) {
        super.init(nibName: nil, bundle: nil)
        self.modal = _modal
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Override
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        title = modal?.rwmc ?? "任务详情"
        view.backgroundColor = .bg
        setupUI()
        
        reloadData()
    }
    
    func reloadData() {
        API.getLzglRwedit(withParam: SecurityParam(id: modal.id)) { responseModel in
            self.modal = responseModel.model
            self.title = self.modal?.rwmc ?? "任务详情"
            DispatchQueue.main.async {
                self.baseInfos = [
                    BaseInfoModel(style: BaseInfoStyleEnum.Normal, title: "履职人", detail: self.modal?.lzrmc, status: .top),
                    BaseInfoModel(style: BaseInfoStyleEnum.Normal, title: "任务时间", detail: self.modal?.rwrq, status: .middle),
                    BaseInfoModel(style: BaseInfoStyleEnum.Normal, title: "完成时间", detail: self.modal?.tjsj, status: .bottom)
                ]
                self.tableView.reloadData()
            }
        }
    }
    
    
    // MARK: - Actions
    
    // MARK: - Setup
    func setupUI() {
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
    }
    
    lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorStyle = .none
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        if #available(iOS 15.0, *) {
            tableView.sectionHeaderTopPadding = 0
        }
        tableView.register(BaseInfoCell.self, forCellReuseIdentifier: "SafeCheckNormalCell")
        tableView.register(BaseInfoCell.self, forCellReuseIdentifier: "SafeCheckStyle1Cell")
        return tableView
    }()
}

extension SafeCheckTaskVC: UITableViewDataSource, UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        if let count = modal?.rwjlMbDqList?.count {
            return count + 1
        }
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (section == 0) {
            return baseInfos.count
        } else {
            if let list = modal?.rwjlMbDqList?[section - 1] {
                return list.boscLzglRwjlMbs?.count ?? 0
            }
            return 0
        }
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if (section == 0) {
            return 1
        } else {
            return 50
        }
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if (section == 0) {
            let view = UIView()
            view.backgroundColor = .clear
            return view
        } else {
            if let data = modal?.rwjlMbDqList?[section - 1] {
                if let dqmc = data.dqmc {
                    let view = SectionTitleView(withStyle: SectionTitleStyleEnum.Style1, title: dqmc)
                    view.backgroundColor = .bg
                    return view
                }
            }
        }

        let view = UIView()
        view.backgroundColor = .clear
        return view
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if (indexPath.section == 0) {
            let data = baseInfos[indexPath.row]
            let cell = (tableView.dequeueReusableCell(withIdentifier: "SafeCheckNormalCell", for: indexPath)) as! BaseInfoCell
            cell.buildData(model: data, status: BaseInfoStatesEnum.Normal.rawValue)
            cell.selectionStyle = .none
            return cell
        } else {
            if let list = modal?.rwjlMbDqList?[indexPath.section - 1] {
                if let data = list.boscLzglRwjlMbs?[indexPath.row] {
                    let cell = (tableView.dequeueReusableCell(withIdentifier: "SafeCheckStyle1Cell", for: indexPath)) as! BaseInfoCell
                    if let rwmc = data.rwmc {
                        var status: MyItemStatus = .middle
                        
                        if list.boscLzglRwjlMbs?.count == 0 {
                            status = .top
                        } else if list.boscLzglRwjlMbs!.count - 1 == indexPath.row {
                            status = .bottom
                        }
                        let msg = "\(self.modal?.lzrmc ?? "")\(self.modal?.tjsj ?? "")"
                        cell.buildData(model: BaseInfoModel(style: .Style1, title: rwmc, msg: msg, status: status), status: data.rwzt)
                    }
                    cell.selectionStyle = .none
                    return cell
                }
            }

        }
        let cell = (tableView.dequeueReusableCell(withIdentifier: "SafeCheckNormalCell", for: indexPath)) as! BaseInfoCell
        cell.selectionStyle = .none
        return cell
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if (indexPath.section == 0) {

        } else {
            if let list = modal.rwjlMbDqList?[indexPath.section - 1] {
                if let data = list.boscLzglRwjlMbs?[indexPath.row] {
                    if data.rwzt != "2" {
                        let dqmc = modal?.rwjlMbDqList?[indexPath.section - 1].dqmc
                        let vc = SafeCheckoutMachinesVC(dModal: data, rModal: modal, title: dqmc)
                        navigationController?.pushViewController(vc, animated: true)
                    }
                }
            }
        }
    }

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        tableView.setCornerRadiusSection(willDisplay: cell, forRowAt: indexPath)
    }
}
